﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApprovalTests;
using ApprovalTests.Reporters;
using ApprovalUtilities.Persistence;
using ApprovalUtilities.Persistence.EntityFramework;
using Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EntityFramework
{
    [TestClass]
    [UseReporter(typeof(DiffReporter))]
    public class Graduation2
    {
        [TestMethod]
        public void TestNewEmployee()
        {
            DatabaseSetup.SetupGraduation();
            Console.WriteLine(Change());
        }
        [TestMethod]
        public void TestLoader()
        {
            using (var db = new EntityFrameworkDemoEntities())
            {
                Approvals.Verify(LoadEmployeesForEvents(db, "Graduation"));
            }
        } 
        [TestMethod]
        public void TestChange()
        {
            var mary = new Employee {Name = "Mary"};
            mary.Events.Add(new Event(){EventTitle = "Graduation"});
            var sally = new Employee {Name = "Sally"};
            sally.Events.Add(new Event(){EventTitle = "Knighthood"});
            var jeff = new Employee {Name = "Jeff"};
            mary.Events.Add(new Event(){EventTitle = "Party"});
            var employees = new Employee[]{mary, sally, jeff};
            var output = Change(new MockLoader<IEnumerable<Employee>>(employees), new MockSaver<IEnumerable<Employee>>());
            Approvals.Verify(output);
        }


        private String Change()
        {
            using (var db = new EntityFrameworkDemoEntities())
            {
                return Change(LoadEmployeesForEvents(db,"Graduation", "Knighthood"),new EntityFrameworkSaver<IEnumerable<Employee>>(db));
            }
        }
        private LambdaArrayLoader<Employee, EntityFrameworkDemoEntities> LoadEmployeesForEvents(EntityFrameworkDemoEntities c, params string[] events)
        {
            return Loaders.Create(c, (db) =>
                                     from e in db.Events
                                     where events.Contains(e.EventTitle)
                                     select e.Employee1);
        }

        private static string Change(ILoader<IEnumerable<Employee>> loader, ISaver<IEnumerable<Employee>> saver)
        {
            string dr = "Dr. ";
            string sir = "Sir ";
            var employees = loader.Load();
            foreach (Employee employee in employees)
            {
                if (employee.HasAttended("Knighthood"))
                {
                    employee.Name = sir + employee.Name;
                }
                else if (employee.HasAttended("Graduation"))
                {
                    employee.Name = dr + employee.Name;
                }

            }
            saver.Save(employees);
            var output = new StringBuilder();
            output.AppendLine("changed the names of:");
            foreach (Employee employee in employees)
            {
                output.AppendLine(employee.Name);
            }
            return output.ToString();
        }
    }
}